$(function () {

    var up = $("#up");
    var down = $("#down");
    var first = $("#first");
    var second = $("#second");
    var third = $("#third");
    var last = $("#last");
    var currentNo = $("#pageNo");
    var pageNo = 1;

    var p1 = first.attr("name");
    var p2 = second.attr("name");
    var p3 = third.attr("name");
    var form = $("#form");

    up.bind("click",function () {
        pageNo = Number(currentNo.val())-1;
        if(pageNo>1){
            currentNo.val(pageNo);
            sub(pageNo)
        }else {
            currentNo.val(1);
            sub(1);
        }
    });

    down.bind("click",function () {
        pageNo = Number(currentNo.val())+1;
        if(pageNo<last.attr("name")){
            currentNo.val(pageNo);
            sub(pageNo);
        } else{
            currentNo.val(last.attr("name"));
            sub(last.attr("name"));
        }
    });

    last.bind("click",function () {
        var vlast = last.attr("name");
        sub(vlast);
    });

    first.bind("click",function () {
        currentNo.val(1);
        sub(p1);
    });

    second.bind("click",function () {
        currentNo.val(p2);
        sub(p2);
    });

    third.bind("click",function () {
        currentNo.val(p3);
        sub(p3);
    });

    //默认获取前五条数据
    $.ajax({
        url : "easybuy/product/productList",
        type : "GET",
        data :  {"pageIndex":pageNo},
        dataType : "json",
        success : function (data) {
            last.attr("name",data.pageCount);
            var th = "<table class='mem_tab' style=\"width:1400px;  text-align:center; margin-top:20px;\" cellspacing=\"0\" cellpadding=\"0\">";
            th +="<tr style='font-size: 20px;height: 60px'><td style='width: 240px'>商品名称</td>";
            th +="<td width='240'>商品图片</td>";
            th +="<td width='240'>库存</td>";
            th +="<td width='240'>价格</td>";
            th +="<td colspan='2'>操作</td></tr>";
            form.append(th);
            for(var i=0;i<data.list.length;i++){
                form.append("<tr style='font-size: 20px;height: 60px'><td>"+data.list[i].name+"</td>"+
                    "<td>"+"<img style='width: 60px;height: 60px' src='../images/"+data.list[i].fileName+"'></td>"+
                    "<td>"+data.list[i].stock+"</td>"+
                    "<td>"+accounting.formatMoney(data.list[i].price, "",1, ",", ".")+"</td>"+
                    "<td>"+"<a href='javascript:void(0)' onclick='del("+data.list[i].id+")'>删除</a>"+"</td>"+
                    "<td>"+"<a href='Product_Update.html?id="+data.list[i].id+"'>修改</a>"+"</td></tr></table>"
                );
            }
        },
        error : function () {
            alert("出现错误");
        }
    });

    function sub(pageNo){
        form.html("");
        //alert($("#pageNo").val());
        $.ajax({
            url : "easybuy/product/productList",
            type : "GET",
            data : {"pageIndex":pageNo},
            dataType: "json",
            success : function(data){

                var th = "<table class='mem_tab' style=\"width:1400px; text-align:center; margin-top:20px;\" cellspacing=\"0\" cellpadding=\"0\">";
                th +="<tr style='font-size: 20px;height: 100px'><td style='width: 240px'>商品名称</td>";
                th +="<td width='240'>商品图片</td>";
                th +="<td width='140'>库存</td>";
                th +="<td width='240'>价格</td>";
                th +="<td colspan='2' >操作</td></tr>";
                form.append(th);
                for(var i=0;i<data.list.length;i++){
                    form.append("<tr style='font-size: 20px;height: 60px'><td>"+data.list[i].name+"</td>"+
                        "<td>"+"<img style='width: 60px;height: 60px' src='../images/"+data.list[i].fileName+"'></td>"+
                        "<td>"+data.list[i].stock+"</td>"+
                        "<td>"+accounting.formatMoney(data.list[i].price,"", 1, ",", ".")+"</td>"+
                        "<td>"+"<a href='javascript:void(0)' onclick='del("+data.list[i].id+")'>删除</a>"+"</td>"+
                        "<td>"+"<a href='Product_Update.html?id="+data.list[i].id+"'>修改</a>"+"</td></tr></table>"
                    );
                }
            },
            error : function(){
                alert("出现错误");
            }
        });
    }

});
function del(id) {
    $.ajax({
        url : "easybuy/product/delete",
        type : "get",
        data : {id : id},
        dataType : "json",
        success : function(data) {
            if (data.result="success") {
                alert("删除成功");

                location.reload();
            }
        },
        error : function () {
            alert("出现错误");
        }
    });
}